﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseData;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class InsuranceCompanyController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        #region   承保公司列表 + JsonResult Index(BaseQueryParams model)
        [HttpPost]
        public JsonResult Index(BaseQueryParams model)
        {
            var total = 0;
            var list = ServiceFactory.InsuranceCompanyServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.CompanyName,
                o.ParentCompanyName,
                o.Remark,
                o.CreateBy,
                o.CreateTime,
                o.LastModifyBy,
                o.LastModifyTime,
                o.IsAvailable
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
#endregion
        #region  承保公司详情页面 + ActionResult Edit(int id)
        public ActionResult Edit(int id)
        {
            var productBrand = ServiceFactory.InsuranceCompanyServiceInstance.Load(id);
            return View(productBrand);
        }
        #endregion
        #region  承保公司 编辑和添加 + ActionResult  Save(InsuranceCompany model)
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Save(InsuranceCompany model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            if (model.ID == 0)
            {
                if (ServiceFactory.InsuranceCompanyServiceInstance.IsExist(model.CompanyName))
                {
                    responseResult.Message = "该承保公司已经存在！";
                }
                else
                {
                    model.CreateBy = CurrentUser.UserAccount;
                    model.CreateTime=DateTime.Now;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    if (model.ParentID > 0)
                        model.ParentCompanyName =
                            ServiceFactory.InsuranceCompanyServiceInstance.Load(model.ParentID).CompanyName;
                    var entity = ServiceFactory.InsuranceCompanyServiceInstance.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            else
            {
                var entity = ServiceFactory.InsuranceCompanyServiceInstance.Load(model.ID);
                if (entity != null)
                {
                    if (ServiceFactory.InsuranceCompanyServiceInstance.IsExist(model.CompanyName, model.ID))
                    {
                        responseResult.Message = "已存在同名的承保公司，请修改承保公司名！";
                    }
                    entity.CompanyName = model.CompanyName;
                    entity.IsAvailable = model.IsAvailable;
                    entity.LastModifyBy = CurrentUser.UserAccount;
                    entity.LastModifyTime =DateTime.Now;
                    if (model.ParentID > 0)
                        model.ParentCompanyName =
                            ServiceFactory.InsuranceCompanyServiceInstance.Load(model.ParentID).CompanyName;
                    responseResult.Success = ServiceFactory.InsuranceCompanyServiceInstance.UpdateEntities(entity);
                }
                else
                {
                    responseResult.Message = "该承保公司不存在或已被删除！";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.ID });
        }
        #endregion
        #region 修改状态 JsonResult UpdateStatus(int id, bool isAvailable)
        [HttpPost]
        public JsonResult UpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.InsuranceCompanyServiceInstance.Load(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                success = ServiceFactory.InsuranceCompanyServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
#endregion
    }
}